package store

import (
	"github.com/go-kit/kit/metrics"
	"github.com/go-kit/kit/metrics/discard"
	"github.com/go-kit/kit/metrics/prometheus"
	stdprometheus "github.com/prometheus/client_golang/prometheus"
)

const (
	// MetricsSubsystem is a subsystem shared by all metrics exposed by this
	// package.
	MetricsSubsystem = "store"
)

// Metrics contains metrics exposed by this package.
// see MetricsProvider for descriptions.
type Metrics struct {
	PartSize metrics.Gauge

	ByteSize metrics.Gauge

	StoredBlocks metrics.Counter

	StoredParts metrics.Counter
}

func PrometheusMetrics(namespace string, labelsAndValues ...string) *Metrics {
	var labels []string
	for i := 0; i < len(labelsAndValues); i += 2 {
		labels = append(labels, labelsAndValues[i])
	}
	return &Metrics{
		PartSize: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
			Namespace: namespace,
			Subsystem: MetricsSubsystem,
			Name:      "part_size",
			Help:      "Size of the part.",
		}, labels).With(labelsAndValues...),

		ByteSize: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
			Namespace: namespace,
			Subsystem: MetricsSubsystem,
			Name:      "byte_size",
			Help:      "Size of the pb block.",
		}, labels).With(labelsAndValues...),

		StoredBlocks: prometheus.NewCounterFrom(stdprometheus.CounterOpts{
			Namespace: namespace,
			Subsystem: MetricsSubsystem,
			Name:      "stored_blocks",
			Help:      "Number of stored blocks.",
		}, labels).With(labelsAndValues...),

		StoredParts: prometheus.NewCounterFrom(stdprometheus.CounterOpts{
			Namespace: namespace,
			Subsystem: MetricsSubsystem,
			Name:      "stored_parts",
			Help:      "Number of stored parts.",
		}, labels).With(labelsAndValues...),
	}
}

// NopMetrics returns no-op Metrics.
func NopMetrics() *Metrics {
	return &Metrics{
		PartSize:     discard.NewGauge(),
		ByteSize:     discard.NewGauge(),
		StoredBlocks: discard.NewCounter(),
		StoredParts:  discard.NewCounter(),
	}
}
